<?php

namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Model;
use think\Session;
use think\Config;

class User extends Base
{

        //登录web
        private $RequestUrl = "https://wechat.whcmlabs.com/home/login";

        public function index(){
            return view();
        }
    //复制用户
    public function copy(){
            $str= $_POST['preuseorlogid'];
            $logid = $_POST['logid'];
            $username =$_POST['username'];
        if(!Model('common')->IsAdminUser()){
            $this->error('权限不够','user/edit');
        }
        if(!trim($str)) {
            $this->error('用户名或者工号-不能为空失败');
        }
        if(!trim($logid)) {
            $this->error('新增员工工号不能为空失败');
        }
        if(!trim($username)) {
            $this->error('新增员工姓名不能为空失败');
        }

        if(Model('common')->IdOrUsername($str)){
            //输入的是姓名
        /*    $res = Model('User')->where('username',$str)->find();*/ //这种是错误的
            $res = Db::table('xt_user')->where('username',$str)->find();
            if(!$res){
                $this->error('没有当前姓名的用户','user/index');
                    }

            //{username:'lis',password:'665544',remember:'false'}

            $res['logid']  = $logid;
            $res['username'] = $username;
            $res['password'] = $logid;
            //统计最近的添加时间 方便设计签名
            //$res['passworddate'] = date('Y-m-d h:i:s', time());
            unset($res['ROW_NUMBER']);
            $status = Model('user')::create($res);
            //这里增加只能是数组 ，如果是 model()得出的数据集 就会报数据表字段不存在:[table]

            //生成电子签名的  配置文件
            Config::set('Usersign.username',$username);

            //这儿自动访问 提交数据 避免登录刷新
            $ac = new Curl();
            $param = array('username' => $logid, 'password' => $logid, 'remember' => 'false');
            $cc = $ac->request_post($url='https://wechat.whcmlabs.com/home/login',$param);



            //这儿开始添加判断是否业务人员
            if(!empty($_POST['ywy'])){
                $c = $this->saleadd($logid,$username);
                Model('Log')->addLog("增加了销售人员.$username", 1);
            }
            if($status){
                //添加成功 保存到log
                Model('Log')->addLog("新增了一个人员$username", 1);

                $this->success('添加成功','user/index');
            }
                $this->success('添加失败','user/index');

        }else{
            //输入的是工号
            $res = Db::table('xt_user')->where('logid',$str)->find();
            /*$res = Model('User')->where('logid',$str)->find();*/
            if(!$res){
                $this->error('没有当前工号的用户','user/index');
            }
            $res['logid']  = $logid;
            $res['password'] = $logid;
            $res['username'] = $username;
            //统计最近的添加时间 方便设计签名
            //$res['passworddate'] = date('Y-m-d h:i:s', time());
            unset($res['ROW_NUMBER']);
            $status = Model('user')::create($res);

            //生成电子签名的  配置文件
            Config::set('Usersign.username',$username);

            //这儿自动访问 提交数据 避免登录刷新
            $ac = new Curl();
            $param = array('username' => $logid, 'password' => $logid, 'remember' => 'false');
            $cc = $ac->request_post($url='https://wechat.whcmlabs.com/home/login',$param);

            //这儿开始添加判断是否业务人员
            if(!empty($_POST['ywy'])){
                $c = $this->saleadd($logid,$username);
                Model('Log')->addLog("增加了销售人员.$username", 1);
            }
            if($status){
                //添加成功 保存到log
                Model('Log')->addLog("新增了一个人员$username", 1);
                $this->success('添加成功','user/index');
            }
            $this->success('添加失败','user/index');
        }
    }

    public function saleadd($card,$name){
        $res = Db::table('xt_brlb')->order('dh desc')->find();
        $ywydh = 'K900'.(substr($res['dh'],2)+1);
        $data = Db::table('xt_brlb')->insert(['dh'=>$ywydh,'mc'=>$name,
            'pym'=>'auto','loginname'=>$card,'password'=>$card,'isuse'=>1]);
        if(!$data){
            return false;
        }
        return true;
    }
    public  function edit(){
            return view();
    }
    /**
     *查询用户
     */
    public function getUserInfo(){
        $data = input('post.');
        if(empty($data['str'])){
            $this->error('不能为空','user/edit');
    }
        if(!Model('common')->IsAdminUser()){
            $this->error('权限不够','user/edit');
        }
        $res =Model('common')->IdOrUsername($data['str']);
        if($res){
                //汉字--->用户名
            if((array)$data = Model('user')->getUserInfoByUsername($data['str'])){
                $this->assign('data',$data);
                return $this->fetch('user/edit');
            }else{
                $this->error('不存在该姓名的用户','user/edit');
            }
        }else{
            //没有汉字-->工号
            if($data =  Model('user')->getUserInfoByLogid($data['str'])){

            $this->assign('data',$data);
            return $this->fetch('user/edit');
            }else{
                $this->error('不存在该工号的用户','user/edit');
            }
        }
    }


    //更新用户信息
    public  function updateInfo(){
        $data = input('post.');
        if(empty($data['logid']) && empty($data['username']) && empty($data['password']) && empty($data['status'])){
            $this->error('不能为空','user/edit');
        }
        if(!Model('common')->IsAdminUser()){
            $this->error('权限不够','user/edit');
        }
        $user = \app\index\model\User::get($data['logid']);
        $user->username = trim($data['username']);
        $user->password = trim($data['password']);
        $user->status = trim($data['status']);
        $user->AccountLock = trim($data['AccountLock']);
        if(false !== $user->save()){
            //更新了记录
            Model('Log')->addLog("更新了.$user->username.的用户账号记录", 1);
            $this->success('更新成功','user/edit');
        }
        $this->error('更新错误','user/edit');
    }


    /**
     *
     */
    //删除离职人员
    public function UserDel(){
        $data['dh'] = $_POST['dh'];
        $cc = $data['dh'];
        if(empty($data['dh'])){
            show('0','不能为空',0);
        }
        if(!Model('common')->IsAdminUser()){
            show('0','权限不够',0);
        }
        $res =Model('common')->IdOrUsername($data['dh']);
        if($res){
            //汉字--->用户名
            if((array)$data = Model('user')->DelUserInfoByUsername($data['dh'])){
                //去删除销售人员
                Db::table('xt_brlb')->where('mc',$cc)->delete();
                //开始记录
                Model('Log')->addLog("删除了一个离职人员.$cc", 1);

                show('1','删除成功',1);
            }else{
                show('1','删除失败 不存在该用户',0);
            }
        }else{
            //没有汉字-->工号
            if($data =  Model('user')->DelUserInfoByLogid($data['dh'])){
                //去删除销售人员 --根据工号
                Db::execute("delete from xt_brlb where loginname = :loginname",['loginname'=>$cc]);
                Model('Log')->addLog("删除了一个离职人员.$cc", 1);
                show('1','删除成功',1);
            }else{
                show('1','删除失败 不存在该用户',0);
            }
        }
    }

    /**
     * 修改医院业务员
     */
    public  function sale(){
        return view();
    }

    /***
     * 修改业务员
     */
    public function alsale(){
        $ywy = $_POST['ywy'];
        $mc = $_POST['mc'].'%';
        if(empty($ywy) || empty($mc)){
            $this->error('不能为空');
        }
        $data['mc'] =$ywy;
        $data['dh'] = $ywy;
        $res = Db::table('xt_brlb')->whereor($data)->select();
        if(!$res){
            $this->error('没有这个 信息 的业务员');
        }
        foreach ($res as $key=>$val){
            /*$cc = DB::table('xt_yymc_print')
                ->execute("update xt_yymc_print set ywy = ?  where dh like '?' or mc like '?'",
                    [$val['dh'], $mc,$mc]);*/
            $aa['mc'] = ['like',$mc];
            $aa['dh'] = ['like',$mc];
            $cc = Db::table('xt_yymc_print')->whereOr($aa)->update(['ywy'=> $val['dh']]);
            //开始记录
            Model('Log')->addLog("修改了地区：$mc.的业务员.$ywy**", 1, $ywy);
        }
        if(!$cc){
            $this->error('修改失败');
        }
        $this->error('修改成功');
    }


}